# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Valery Kartel <valery.kartel@gmail.com>
pkgname=litespeed
pkgver=1.4.50
pkgrel=1
_pkgreal=open$pkgname
_pkghome=var/lib/$pkgname
_php=php7
pkgdesc="High-performance, lightweight, open source HTTP server"
url="https://openlitespeed.org/"
arch=""
license="GPL-3.0-or-later"
pkgusers=litespeed
pkggroups=litespeed
depends="$_php-$pkgname $_php-bcmath $_php-json $_php-mcrypt $_php-session $_php-sockets $_php-posix"
makedepends="linux-headers openssl-dev geoip-dev expat-dev pcre-dev zlib-dev
	bsd-compat-headers lua-dev luajit-dev brotli-dev udns-dev"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc $pkgname-snmp::noarch"
source="https://openlitespeed.org/packages/openlitespeed-$pkgver.src.tgz
	$pkgname.initd
	include.patch
	install.patch
	ls_lock.patch
	thread.patch
	"
builddir="$srcdir/openlitespeed-$pkgver"

build() {
	./configure \
		--host=$CHOST \
		--build=$CBUILD \
		--prefix=/$_pkghome \
		--with-user=$pkgusers \
		--with-group=$pkggroups \
		--enable-adminssl=no \
		--disable-rpath \
		--disable-static \
		--with-openssl=/usr \
		--with-expat \
		--with-pcre \
		--with-lua \
		--with-brotli=/usr/lib/ \
		--with-zlib
	make
}

package() {
	local file;
	make DESTDIR="$pkgdir" install

	mkdir -p "$pkgdir"/usr/lib/$pkgname \
		"$pkgdir"/usr/sbin \
		"$pkgdir"/var/log

	# remove trash
	rm -fr "$pkgdir"/$_pkghome/php* \
		"$pkgdir"/$_pkghome/lib \
		"$pkgdir"/$_pkghome/GPL* \
		"$pkgdir"/$_pkghome/gdata \
		"$pkgdir"/$_pkghome/autoupdate \
		"$pkgdir"/$_pkghome/fcgi-bin/* \
		"$pkgdir"/$_pkghome/bin/lshttpd \
		"$pkgdir"/$_pkghome/admin/conf/php.* \
		"$pkgdir"/$_pkghome/admin/misc/gdb-bt \
		"$pkgdir"/$_pkghome/admin/misc/convertxml.* \
		"$pkgdir"/$_pkghome/admin/misc/build_admin_php.sh

	# fix ownership
	chown -R $pkgusers:$pkggroups \
		"$pkgdir"/$_pkghome/tmp \
		"$pkgdir"/$_pkghome/conf \
		"$pkgdir"/$_pkghome/logs \
		"$pkgdir"/$_pkghome/backup \
		"$pkgdir"/$_pkghome/cachedata \
		"$pkgdir"/$_pkghome/admin/tmp \
		"$pkgdir"/$_pkghome/admin/logs \
		"$pkgdir"/$_pkghome/admin/conf \
		"$pkgdir"/$_pkghome/admin/cgid \
		"$pkgdir"/$_pkghome/Example/logs

	# install configs
	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
	mv "$pkgdir"/$_pkghome/conf \
		"$pkgdir"/etc/$pkgname
	mv "$pkgdir"/$_pkghome/admin/conf \
		"$pkgdir"/etc/$pkgname/admin
	ln -s /etc/$pkgname "$pkgdir"/$_pkghome/conf
	ln -s /etc/$pkgname/admin "$pkgdir"/$_pkghome/admin/conf
	find "$pkgdir"/etc/$pkgname -type f -print0 | xargs -0 chmod -x

	# install binary
	mv "$pkgdir"/$_pkghome/bin/$_pkgreal \
		"$pkgdir"/usr/sbin/lshttpd
	ln -sf /usr/sbin/lshttpd \
		"$pkgdir"/$_pkghome/bin/$pkgname

	# install modules
	for file in $(find "$pkgdir"/$_pkghome/modules -name "*.so"); do
		mv $file "$pkgdir"/usr/lib/$pkgname/${file##*/}
		ln -s /usr/lib/$pkgname/${file##*/} $file
	done

	# install logs
	mv "$pkgdir"/$_pkghome/logs "$pkgdir"/var/log/$pkgname
	mv "$pkgdir"/$_pkghome/admin/logs "$pkgdir"/var/log/$pkgname/admin
	mv "$pkgdir"/$_pkghome/Example/logs "$pkgdir"/var/log/$pkgname/Example
	ln -s /var/log/$pkgname "$pkgdir"/$_pkghome/logs
	ln -s /var/log/$pkgname/admin "$pkgdir"/$_pkghome/admin/logs
	ln -s /var/log/$pkgname/Example "$pkgdir"/$_pkghome/Example/logs

	# install backend
	ln -s /usr/bin/ls$_php "$pkgdir"/$_pkghome/fcgi-bin/lsphp
	ln -s /etc/$_php/php.ini "$pkgdir"/etc/$pkgname/php.ini
	ln -s /etc/$_php/php.ini "$pkgdir"/etc/$pkgname/admin/php.ini
}

snmp() {
	pkgdesc="$pkgdesc (snmp monitoring add-on + cacti templates)"
	depends="$pkgname net-snmp"

	mkdir -p "$subpkgdir"/$_pkghome/add-ons
	mv "$pkgdir"/$_pkghome/add-ons/snmp_monitoring \
		"$subpkgdir"/$_pkghome/add-ons
}

sha512sums="1d16eec757876f55950afa09d9aeb454b18b4446ca859ee26c73a5200a0bea5e666283812bb35345b2d2a807d20df0095aceca32ebd16c84316ae5c0111c6cba  openlitespeed-1.4.50.src.tgz
2b0299246aaeb885122d8f939437f0e4701f6a79ea976f725ba8765d8b295c2797c0cac30e639d5eda665b9589710ee55acc4d03b078c4b9cc1e564994ccb9f5  litespeed.initd
50c21bf9cc12a8d7408c6e981f3264e4daeff8285f8544c4dbe3673773abde769d4386067d2b4c858b48ba496c384d69ff6aa1eee8291d8c09b213e847d50ff4  include.patch
732bcdd87db538bfb3432abe42b6a7868ef97ef8b2d8063f3875879cb83079cf2a73e3a0a92c7dedfad6b454affabf9739580af9a4098f5fef942b3510a4ad28  install.patch
d17fe323aa3dde1ce881c6bac8775abf1b45ed83db57f6a3dc1c458e48e0d42ad65944bd364590b79fef425f3ff55966586f9760396af4556d25f06f4a70b7ad  ls_lock.patch
c7775836ee13b082e5c2ce011673b1a30b5ca83765d6d81ff1359110ba970808de6a87cd6d61fd688c9d2037451598154b429d70ad25094b9e9f10938aea00fd  thread.patch"
